package org.csu.mall.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Shop;
import org.csu.mall.entity.User;
import org.csu.mall.persistence.ShopMapper;
import org.csu.mall.persistence.UserMapper;
import org.csu.mall.service.IShopService;
import org.csu.mall.utils.UsernameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class ShopServiceImpl implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public UniformResponse<String> checkShopIsOpen() {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            User user = userMapper.selectOne(
                    Wrappers.<User>query().eq("id",userid));
            int shopId=user.getShopId();
            if (shopId == CONSTANT.SHOPID){
                return UniformResponse.createForSuccessMessage("该用户未开店");
            }
            else {
                return UniformResponse.createForErrorMessage("该用户已开店");
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public UniformResponse<Shop> openShop(Shop shop) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        User user = userMapper.selectOne(
                Wrappers.<User>query().eq("id",userid));
        if (this.checkShopIsOpen().isSuccess()) {
            shop.setUserId(userid);
            shop.setCreateTime(LocalDateTime.now());
            shop.setUpdateTime(LocalDateTime.now());

            int rows = shopMapper.insert(shop);
            if (rows == 0) {
                return UniformResponse.createForErrorMessage("开店失败");
            }
            user.setRole(CONSTANT.Role.SHOPKEEPER);
            user.setShopId(shop.getId());
            int userrow = userMapper.updateById(user);
            if (userrow == 0) {
                return UniformResponse.createForErrorMessage("用户未被设置为店铺管理员");
            }
            return UniformResponse.createForSuccess("开店成功", shop);
        }
        else {
            return UniformResponse.createForErrorMessage("该用户已开店");
        }
    }

    @Override
    public UniformResponse<Shop> searchShop() {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Shop shop = shopMapper.selectOne(
                    Wrappers.<Shop>query().eq("user_id",userid));
            if(shop == null){
                return UniformResponse.createForErrorMessage("店铺不存在");
            }
            return UniformResponse.createForSuccess("店铺信息查询成功",shop);
        }
    }

    @Override
    public UniformResponse<Shop> updateShop(Shop shop) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            shop.setUserId(userid);
            shop.setUpdateTime(LocalDateTime.now());
            int rows=shopMapper.updateById(shop);
            if(rows == 0){
                return UniformResponse.createForErrorMessage("店铺信息修改失败");
            }
            return UniformResponse.createForSuccess("店铺信息修改成功",shop);
        }
    }

    @Override
    public UniformResponse<String> closeShop(String id) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            User user = userMapper.selectOne(
                    Wrappers.<User>query().eq("id",userid));
            user.setRole(CONSTANT.Role.CUSTOMER);
            user.setShopId(CONSTANT.SHOPID);
            int userrow = userMapper.updateById(user);
            if (userrow == 0) {
                return UniformResponse.createForErrorMessage("用户未被设置为消费者");
            }
            Map<String, Object> columnMap = new HashMap<String, Object>();
            columnMap.put("user_id", userid);
            columnMap.put("id", id);
            int rows=shopMapper.deleteByMap(columnMap);
            if(rows == 0){
                return UniformResponse.createForErrorMessage("店铺关闭失败");
            }
            return UniformResponse.createForSuccessMessage("店铺关闭成功");
        }
    }
}
